package com.tianhui.consignortwofei.mvp.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonView;
import com.fgs.common.widget.TabSwitchView;
import com.fgs.common.widget.itemView.InputItemView;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseActivity;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.constant.OrderType;
import com.tianhui.consignortwofei.manager.SharedPreferenceManager;
import com.tianhui.consignortwofei.mvp.model.enty.AdjustInfo;
import com.tianhui.consignortwofei.mvp.model.enty.OrderInfo;
import com.tianhui.consignortwofei.pattern.orderAdjust.IOrderAdjust;
import com.tianhui.consignortwofei.pattern.orderAdjust.impl.DeliverConfirm;
import com.tianhui.consignortwofei.pattern.orderAdjust.impl.LoadingConfirm;
import com.tianhui.consignortwofei.pattern.orderAdjust.impl.PriceAdjust;
import com.tianhui.consignortwofei.pattern.orderAdjust.impl.QuantityAdjust;
import com.tianhui.consignortwofei.pattern.orderAdjust.impl.RoadAdjust;
import com.tianhui.consignortwofei.pattern.orderAdjust.impl.UnloadTimeAdjust;
import com.tianhui.consignortwofei.pattern.orderAdjust.impl.WeightAdjust;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2020/9/1
 * @description
 **/
public class OrderAdjustActivity extends BaseActivity {

    @BindView(R.id.layout_source_adjust_originalTipTextView)
    TextView mOriginalTipTextView;
    @BindView(R.id.layout_source_adjust_taxOriginalTipTextView)
    TextView mTaxOriginalTipTextView;
    @BindView(R.id.layout_source_adjust_originalInputItemView)
    InputItemView mOriginalInputItemView;
    @BindView(R.id.layout_source_adjust_roadTypeTabSwitchView)
    TabSwitchView mRoadTypeTabSwitchView;
    @BindView(R.id.layout_source_adjust_adjustTipTextView)
    TextView mAdjustTipTextView;
    @BindView(R.id.layout_source_adjust_adjustInputItemView)
    InputItemView mAdjustInputItemView;
    @BindView(R.id.layout_source_adjust_roadUnitTextView)
    TextView mRoadUnitTextView;
    @BindView(R.id.layout_source_adjust_remarkEditText)
    EditText mRemarkEditText;

    @BindView(R.id.layout_source_adjust_priceStatusLinearLayout)
    LinearLayout mPriceStatusLinearLayout;
    @BindView(R.id.layout_source_adjust_priceStatusTabSwitchView)
    TabSwitchView mPriceStatusTabSwitchView;
    @BindView(R.id.layout_source_adjust_phoneInputItemView)
    InputItemView mPhoneInputItemView;
    private IOrderAdjust iOrderAdjust;
    private OrderInfo orderInfo;

    private int mRoadType;
    private int dingJinType;

    @Override
    public int getLayoutId() {
        return R.layout.activity_order_adjust;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public void init() {

        Bundle bundle = getIntent().getExtras();
        if (bundle != null && !bundle.isEmpty()) {
            int type = bundle.getInt(KeyValue.KEY_TYPE);
            orderInfo = bundle.getParcelable(KeyValue.KEY_ORDER_INFO);
            iOrderAdjust = createOrderAdjustStatus(type);
            if (iOrderAdjust != null && orderInfo != null) {
                getToolbarStatus().setTitle(iOrderAdjust.getAdjustTitle());
                mOriginalTipTextView.setText(iOrderAdjust.getOriginalText());
                mOriginalInputItemView.setContent(iOrderAdjust.getOriginalValue(orderInfo));
                mAdjustTipTextView.setText(iOrderAdjust.getAdjustText(orderInfo));
                mAdjustInputItemView.setContentHint(iOrderAdjust.getOriginalValueInputTip());
                mAdjustInputItemView.setInputType(iOrderAdjust.getInputItemType());
                mAdjustInputItemView.setContent(iOrderAdjust.getOriginalValue(orderInfo));

                if (iOrderAdjust.isShowRemark()) {
                    mRemarkEditText.setVisibility(View.VISIBLE);
                } else {
                    mRemarkEditText.setVisibility(View.GONE);
                }

                dingJinType = orderInfo.dingJinType;
            }
        }

        initListener();

    }

    private void initListener() {

        getWindow().getDecorView().setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                hideKeyboard();
                mAdjustInputItemView.getEditTextView().clearFocus();
                return false;
            }
        });

        mRoadTypeTabSwitchView.setOnTabCheckedChangeListener(new TabSwitchView.OnTabCheckedChangeListener() {
            @Override
            public void onCheckedChanged(int position, String item, String value) {
                if (position == 0) { //定额
                    mRoadType = 0;
                } else {//定率
                    mRoadType = 1;
                }
                showRoadTipView(position);
            }
        });
        mAdjustInputItemView.getEditTextView().setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                EditText textView = (EditText) v;
                String hint;
                if (hasFocus) {
                    hint = textView.getHint().toString();
                    textView.setTag(hint);
                    textView.setHint("");
                } else {
                    hint = textView.getTag().toString();
                    textView.setHint(hint);
                }
            }
        });

        mPriceStatusTabSwitchView.setOnTabCheckedChangeListener(new TabSwitchView.OnTabCheckedChangeListener() {
            @Override
            public void onCheckedChanged(int position, String item, String value) {
                orderInfo.ishide = value;
                showBargainPhone(position == 1);
            }
        });
    }

    //是否显示议价电话View
    private void showBargainPhone(boolean isShow) {
        if (isShow) {
            mPhoneInputItemView.setVisibility(View.VISIBLE);
            mPhoneInputItemView.setContent(SharedPreferenceManager.getInstance().getPhone());
        } else {
            mPhoneInputItemView.setVisibility(View.GONE);
            mPhoneInputItemView.setContent("");
        }
    }

    // type 0 定额 1 定率
    private void showRoadTipView(int type) {
        if (type == 0) {
            mAdjustTipTextView.setText("路耗计量（定额）");
            mRoadUnitTextView.setText("kg/车");
        } else {
            mAdjustTipTextView.setText("路耗计量（定率）");
            mRoadUnitTextView.setText("‰/车");
        }
    }

    private IOrderAdjust createOrderAdjustStatus(int type) {


        switch (type) {
            case OrderType.TYPE_ADJUST_PRICE:
                mTaxOriginalTipTextView.setVisibility(View.VISIBLE);
                mTaxOriginalTipTextView.setText("（含税）价格 " + orderInfo.taxprice);
                showPriceStatusView();
                return new PriceAdjust();
            case OrderType.TYPE_ADJUST_WEIGHT:
                return new WeightAdjust();
            case OrderType.TYPE_ADJUST_QUANTITY:
                return new QuantityAdjust();
            case OrderType.TYPE_LOADING_CONFIRM:
                LoadingConfirm loadingConfirm = new LoadingConfirm();
                mAdjustInputItemView.setContent(loadingConfirm.getOriginalValue(orderInfo));
                return loadingConfirm;
            case OrderType.TYPE_DELIVER_CONFIRM:
                DeliverConfirm deliverConfirm = new DeliverConfirm();
                mAdjustInputItemView.setContent(deliverConfirm.getOriginalValue(orderInfo));
                return deliverConfirm;
            case OrderType.TYPE_ADJUST_ROAD:
                showAdjustRoadView();
                RoadAdjust roadAdjust = new RoadAdjust();
//                mAdjustInputItemView.setContent(roadAdjust.getOriginalValue(orderInfo));
                return roadAdjust;
          case OrderType.TYPE_UNLOAD_TIME:
                return new UnloadTimeAdjust();
        }
        return null;
    }

    private void showPriceStatusView() {
        mPriceStatusLinearLayout.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(orderInfo.ishide) && orderInfo.ishide.equals("1")) {
            mPriceStatusTabSwitchView.setDefaultSelection(1);
            mPhoneInputItemView.setVisibility(View.VISIBLE);
            mPhoneInputItemView.setContent(orderInfo.facetel);
        } else {
            mPriceStatusTabSwitchView.setDefaultSelection(0);
            mPhoneInputItemView.setVisibility(View.GONE);
        }
    }

    private void showAdjustRoadView() {
        mOriginalInputItemView.setVisibility(View.GONE);
        mRoadTypeTabSwitchView.setVisibility(View.VISIBLE);
        mRoadUnitTextView.setVisibility(View.VISIBLE);
        mRoadTypeTabSwitchView.setDefaultSelection(0);
        if (!TextUtils.isEmpty(orderInfo.consumequota)) {
            mRoadTypeTabSwitchView.setDefaultSelection(0);
            mAdjustInputItemView.setContent(orderInfo.consumequota);
            mRoadUnitTextView.setText("kg/车");
        } else if (!TextUtils.isEmpty(orderInfo.consumefixedrate)) {
            mRoadTypeTabSwitchView.setDefaultSelection(1);
            mRoadUnitTextView.setText("‰/车");
            mAdjustInputItemView.setContent(orderInfo.consumefixedrate);
        }
    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }

    @OnClick(R.id.layout_source_adjust_submitTextView)
    public void submit() {

        if (iOrderAdjust != null) {

            if (orderInfo.ishide.equals("1")) {
                String phone = mPhoneInputItemView.getContent();
                orderInfo.facetel = phone;
            }

            AdjustInfo adjustInfo = new AdjustInfo();
            String adjustValue = mAdjustInputItemView.getContent();
            String remark = mRemarkEditText.getText().toString().trim();
            adjustInfo.adjustValue = adjustValue;
            adjustInfo.remark = remark;
            adjustInfo.adjustType = mRoadType;
            iOrderAdjust.adjustOperate(this, adjustInfo, orderInfo);
        }
    }

    @OnClick(R.id.layout_source_adjust_cancelTextView)
    public void cancel() {
        finish();
    }
}
